package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.flag.GameDefine;
	
	import newx.com.config.StaticIni;

	public class TaskLogic
	{
		private var _model 			:ModelLocator;
		
		/**当前任务信息表 表名*/
		private var _curtTaskLablName	:String	=	"Task_Accepted";
		
		/**任务进度表 表名*/
		private var _taskRecord		:String	=	"Task_Record";
		
		public function TaskLogic()
		{
			init();
		}
		
		private function init() :void
		{
			_model = ModelLocator.getInstance();
		}
		
		/**
		 *	获得任务具体内容
		 * @param taskId		任务id
		 * @return 				任务Infoobj
		 */
		public function getTaskInfo(taskId : int):Array
		{
			var taskInfoRec 	:Record = 	getTaskInfoRec();
			var taskInfo 		:Array 	= 	[];
			
			var tSize			:int	=	taskInfoRec.recArr.length;
			
			for(var i:int = 0 ; i< tSize ; i ++)
			{
				if(taskInfoRec.recArr[i][0] == taskId)
				{
					taskInfo = taskInfoRec.recArr[i];
					break;
				}
			}
			return taskInfo;
		}
		
		/**
		 *	获得任务具体进度内容
		 * @param taskId		任务id
		 * @return 				任务Infoobj
		 */
		public function getTaskProgress(taskId : int):Array
		{
			var taskProgressRec 	:Record = 	getTaskProgressRec();
			var taskProgress		:Array 	= 	[];
			
			var tSize				:int	=	taskProgressRec.recArr.length;
			
			for(var i:int = 0 ; i < tSize ; i++)
			{
				if(taskProgressRec.recArr[i][0] == taskId)
				{
					taskProgress = taskProgressRec.recArr[i];
					break;
				}
			}
			return taskProgress;
		}
		
		/**
		 *	解析任务具体内容		
		 * @param taskInfo		任务具体内容		
		 */
		public function resolveTaskInfo(taskInfo : Array):String
		{	
			//暂时没用到，用到在写
			return "";
		}
		
		public function getTaskType(taskInfo : Array) : String
		{	
			return getTaskTypeDisp(StaticIni.getSwfLangStr("Task_Caption" + taskInfo[0]),taskInfo[2]);
		}
		
		/**
		 * 	解析任务进度
		 * @param taskTracking	任务具体进度
		 * @param tracking		任务跟踪
		 * @return 
		 * 
		 */
		public function resolveTaskProgress(taskProgress : Array,tracking:Boolean):String
		{
			if(!haveTaskProgress(taskProgress)) return "";
			
			var curTaskID:int 		= taskProgress[0];		//id
			var curTaskType:int	    = taskProgress[1];		//任务 类型
			var CustomTaskType:int	    = taskProgress[2];		//自定义任务 类型
			var curAmount:int		= taskProgress[3];		// 当前进度
			var totalAmount:int		= taskProgress[4];		// 总进度
			var key:String 			= taskProgress[5];		// 关键字描述
			
			var keyStr:String = "";
//			if(curTaskType == GameDefine.TASKTYPE_CUSTOM)
//			{// 判断是不是自定义任务类型
//				keyStr		= StaticIni.getSwfLangStr("CustomTask_"+CustomTaskType);
//			}else
//			{
//				keyStr		= StaticIni.getSwfLangStr(key);
//			}
			
			// 根据完成状况，设置颜色
			var txtColor:String		= "";
			
			if (curAmount < totalAmount)
			{
				if(!tracking){
					txtColor = StaticIni.getIniVar("Css_txt_Task_schedule", "uncomplete");
				}else{
					txtColor = StaticIni.getIniVar("css_a_TaskTracking", "uncomplete");
				}
			}
			else
			{
				if(!tracking){
					txtColor = StaticIni.getIniVar("Css_txt_Task_schedule", "complete");
				}else{
					txtColor = StaticIni.getIniVar("css_a_TaskTracking", "complete");
				}
			}
			
			// 进度描述
			var scheduleStr:String	= "";
			scheduleStr = StaticIni.getSwfLangStrVar("Task_schedule_1", [txtColor,curAmount,totalAmount,keyStr]);
			
			return scheduleStr ;
		}
		
		/**
		 * 获取所要提交的NPC名称 
		 * @param taskProgress
		 * @return 
		 * 
		 */		
		public function getTaskNPC(val:int):String
		{
			var npcStr		:String		=	"";
			var npcArray	:Array		=	[];
			npcStr				=	StaticIni.getSwfLangStr("Task_SubmitNpc"+ val);
			if(npcStr == "")
			{
				return "";
			}
			npcArray			=	npcStr.split("</a>");
			npcStr				=	StaticIni.getSwfLangStr("Common_taskcansubmit")+":"+ npcArray[0] + "</a>";
			return npcStr;
		}
		
		/**
		 * 获取所要接取的NPC名称 
		 * @param taskProgress
		 * @return 
		 * 
		 */		
		public function giveTaskNPC(val:int):String
		{
			var npcStr		:String		=	"";
			var npcArray	:Array		=	[];
			npcStr				=	StaticIni.getSwfLangStr("Task_AcceptNpc"+ val);
			if(npcStr == "")
			{
				return "";
			}
			npcArray			=	npcStr.split("</a>");
			npcStr				=	StaticIni.getSwfLangStr("Common_taskcanaccept")+":"+ npcArray[0] + "</a>";
			return npcStr;
		}
		/**
		 *	是否有任务进度，没有的不能跟踪 
		 * @param taskProgress
		 * 
		 */
		public function haveTaskProgress(taskProgress : Array):Boolean
		{
			var type:int = taskProgress[1];		// 类型
			// 非猎杀类或收集类任务，无需显示任务进度
			if (type != GameDefine.TASKTYPE_HUNT &&
				type != GameDefine.TASKTYPE_COLLECT&&
				type != GameDefine.TASKTYPE_CUSTOM){
				return false;
			}
			return true;
		}
		
		/**
		 *	获得任务信息表 
		 * @return 
		 * 
		 */
		public function getTaskInfoRec():Record
		{
			return _model.scene.GetRecordEx(_curtTaskLablName);
		}
		
		/**
		 *	获得任务进度表 
		 * @return 
		 * 
		 */
		public function getTaskProgressRec():Record
		{
			return _model.scene.GetRecordEx(_taskRecord);
		}
		
		/**
		 *	获得任务类型对应的配置(比如：支线任务，主线任务) 
		 * 
		 */
		public function getTaskTypeDisp(taskName:String,type:String):String
		{
			var tmpStr:String	=	"";
			switch(type)
			{
				case "1-1":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000449",[taskName]);
					break;
				case "1-10":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000450",[taskName]);
					break;
				case "1-2":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000451",[taskName]);
					break;
				case "1-3":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000452",[taskName]);
					break;
				case "1-4":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000453",[taskName]);
					break;
				case "1-5":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000454",[taskName]);
					break;
				case "1-6":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000455",[taskName]);
					break;
				case "1-7":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000456",[taskName]);
					break;
				case "1-8":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000457",[taskName]);
					break;
				case "1-9":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000458",[taskName]);
					break;
				case "1-11":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000559",[taskName]);
					break;
				case "1-12":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000560",[taskName]);
					break;
				case "1-13":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000561",[taskName]);
					break;
				case "1-14":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000562",[taskName]);
					break;
				case "1-15":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000563",[taskName]);
					break;
				case "1-16":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000581",[taskName]);
					break;
				case "1-16":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1000581",[taskName]);
					break;
				case "1-20":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIStr_1001899",[taskName]);
					break;
				/**奇遇任务**/
				case "1-25":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIString_1000446",[taskName]);
					break ;
				/**精英副本**/
				case "1-30":
					tmpStr	=	StaticIni.getSwfLangStrVar("UIString_1000489",[taskName]);
					break ;
				default:
					tmpStr	=	taskName;
					break;
			}
			return tmpStr;
		}
		/**
		 * 获取顺序号 
		 * @param type
		 * @return 
		 * 
		 */		
		public function getTaskTypeKey(type:String):int
		{
			var key:int	=	0;
			switch(type)
			{
				case "1-1":
					key		=	18;
					break;
				case "1-10":
					key		=	17;
					break;
				case "1-25":
					key		=	16;
					break;
				case "1-20":
					key		=	15;
					break;
				
				case "1-30":
					key		=	14;
					break;
				
				case "1-2":
					key		=	13;
					break;
				case "1-3":
					key		=	12;
					break;
				case "1-4":
					key		=	11;
					break;
				case "1-5":
					key		=	10;
					break;
				case "1-6":
					key		=	9;
					break;
				case "1-7":
					key		=	8;
					break;
				case "1-8":
					key		=	7;
					break;
				case "1-9":
					key		=	6;
					break;
				case "1-11":
					key		=	5;
					break;
				case "1-12":
					key		=	4;
					break;
				case "1-13":
					key		=	3;
					break;
				case "1-14":
					key		=	2;
					break;
				case "1-15":
					key		=	1;
					break;
				case "1-16":
					key		=	0;
					break;
				default:
					key		=	0;
					break;
			}
			return key;
		}
		/**
		 *取得完成后缀 
		 * @param taskID	任务ID
		 * @return 			完成后缀
		 */	
		public function getCompletedSuffix(taskID:int,taskStatus:int):String
		{
			var suffixRtn:String = "";
			
			// 若任务已完成，加上已完成后缀
			if (taskStatus == 1)
			{
				var suffix:String = StaticIni.getSwfLangStr("Common_Complete");
				
				suffixRtn = StaticIni.getSwfLangStrVar("textcolor", 
					["#"+StaticIni.getIniVar("Css_txt_Task_schedule","suffix"), suffix]);
			}
				// 若任务失败，加上失败后缀 
			else if (taskStatus == -1)
			{
				var suffixFail:String = StaticIni.getSwfLangStr("Common_Fail");
				
				suffixRtn = StaticIni.getSwfLangStrVar("textcolor", 
					["#"+StaticIni.getIniVar("Css_txt_Task_schedule","suffix"), suffixFail]);
			}
			
			return suffixRtn;
		}
		/**
		 * 获取主线任务中以完成任务的下一个任务ID
		 * @return
		 */
		public function getNextTaskID():String
		{
			var nextTaskID:String	=	""
			var taskLen:int	=	getTaskInfoRec().recArr.length;
			var taskItem:Array	=	[];
			var taskType:String	=	"";
			var taskStrat:int	=	-1;
			var taskID:int		=	0
			for (var i:int = taskLen; i < 0; i--) 
			{
				taskItem	=	getTaskInfoRec().recArr[i];
				taskID		=	taskItem[0];
				taskType	=	taskItem[1];
				taskStrat	=	taskItem[2];
				if (taskType == "1-1" && taskStrat == 1)
				{
					var taskObj:Object	= StaticIni.getIniObj(String(taskID))	
					if (taskObj != null) 
					{
						nextTaskID	=	taskObj["NextTask"];
					}
				}
			}
			return nextTaskID;
		}
		/**
		 * 更据任务号，获取任务追踪的ID号 
		 * @return 
		 * 
		 */		
		public function getTaskLinkID(val:int):String
		{
			var taskLinkStr:String		=	""
			var linkStr:String			=	StaticIni.getSwfLangStr("Str_" + val);
			var linkCheckArray:Array	=	linkStr.split("event:");
			if(linkCheckArray.length <=1)return "";
			taskLinkStr	=	(linkCheckArray[1] as String).split("'")[0];
			return taskLinkStr;
		}
	}
}